package com.cropdoctor.controller;

import com.cropdoctor.common.Result;
import com.cropdoctor.model.constants.PageParams;
import com.cropdoctor.model.constants.PageResult;
import com.cropdoctor.model.dto.PostDto;
import com.cropdoctor.model.po.CommentPo;
import com.cropdoctor.model.po.PostPo;
import com.cropdoctor.model.vo.PostVO;
import com.cropdoctor.service.CommentService;
import com.cropdoctor.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/forum")
@Api(tags = "论坛相关接口")
public class ForumController {
    @Autowired
    PostService postService;

    @Autowired
    CommentService commentService;

    //#region (论坛模块帖子相关功能)
    @PostMapping("/post")
    @ApiOperation(value = "新建一篇帖子")
    public Result<String> createPost(PostPo postPo, @RequestParam("file") MultipartFile img){
        return postService.createPost(postPo,img);
    }

    @DeleteMapping("/post/{idPost}")
    @ApiOperation(value = "通过帖子的主键id删除相应的帖子")
    public Result<String> userDeletePost(@PathVariable("idPost") long idPost){
        return postService.userDeletePost(idPost);
    }

    @GetMapping("/post/user")
    @ApiOperation(value = "通过用户id，查询该用户发布的所有的帖子")
    public Result<List<PostPo>> getUserAllPost(){
        return postService.getUserAllPost();
    }

    @GetMapping("/post")
    @ApiOperation(value = "获取当前论坛中的帖子")
    public Result<PageResult<PostVO>> getAllPost(@RequestParam(required = false,value = "keyword",defaultValue = "") String keyword,
                                                 @RequestParam int pageNo,
                                                 @RequestParam int pageSize){
        PageParams pageParams = new PageParams(pageNo, pageSize);
        return postService.getAllPost(pageParams,keyword);
    }

    @PostMapping("/post/like/{idPost}")
    public Result<String> likePost(@PathVariable("idPost") long idPost){
        return postService.likePost(idPost);
    }

    //#endregion

    //#region (论坛模块评论相关功能)
    @PostMapping("/comment")
    @ApiOperation(value = "在帖子中新发表一条评论")
    public Result<Long> createComment(@RequestBody CommentPo commentPo){
        return commentService.createComment(commentPo);
    }

    @DeleteMapping("/comment/{idComment}")
    @ApiOperation(value = "根据传入的评论表主键id，删除评论")
    public Result<String> userDeleteComment(@PathVariable("idComment") long idComment){
        return commentService.userDeleteComment(idComment);
    }

    @GetMapping("/post/comment/{idPost}")
    @ApiOperation(value = "获取当前帖子及帖子中的所有评论")
    public Result<PostDto> getPostInfo(@PathVariable("idPost") long idPost){
        return commentService.getPostWithAllComment(idPost);
    }

    //#endregion
}
